import {
	config
} from '../config/index.js'

export const apiResquest = (prams) => { //prams 为我们需要调用的接口API的参数
	// 判断请求类型
	let headerData = {
		'content-type': 'application/json'
	}

	let dataObj = null
	if (prams.method === "GET") {
		headerData = {
			'content-type': 'application/json',
			'token': uni.getStorageSync('token')
		}
	} else {
		headerData = {
			'content-type': 'application/json',
			'token': uni.getStorageSync('token')
		}
		
	}
	return new Promise((resolve, reject) => {
		let url = config.base_url + prams.url; //请求的网络地址和局地的api地址组合
		// uni.showLoading({
		// 	title: '加载中',
		// 	mask: true
		// })
		
		return uni.request({
			url: url,
			data: prams.query,
			method: prams.method,
			header: headerData,
			success: (res) => {
				// uni.hideLoading()
				
				if(prams.url == 'video/watchVideoPay'){
					resolve(res.data);
				}else if (res.data.code === 101) {
					uni.showToast({
						title: res.data.message,
						duration: 1000,
						icon: "none",
						success:function(){
							uni.setStorageSync('hasUserInfo', false)
							uni.setStorageSync('is_online', false)
							uni.setStorageSync('user_info', null)
							uni.setStorageSync('token', '')
						}
					})
					setTimeout(function(){
						uni.reLaunch({
							url:'/pages/My-self/My-self'
						})
					},1000)
					
				}else if(res.data.code !== 200){
					uni.showToast({
						title: res.data.message,
						duration: 1000,
						icon: "none",
					})
					resolve(res.data);
				}else{
					resolve(res.data);
				}
			},
			fail: (err) => {
				// uni.hideLoading()
				reject(err);
				uni.showToast({
					title: "当前网络状态不可用",
					icon: "none"
				})
			}
		});
	})
}
